<template>
  <div class="loading-screen" :class="{ 'fade-out': fadeOut }">
    <div class="loading-container">
      <!-- 游戏Logo -->
      <div class="game-logo">
        <div class="logo-icon">
          <i class="icon-game"></i>
        </div>
        <h1 class="logo-text">Vue跑酷游戏</h1>
      </div>
      
      <!-- 加载动画 -->
      <div class="loading-animation">
        <div class="runner-animation">
          <div class="runner">
            <div class="runner-body"></div>
            <div class="runner-legs">
              <div class="leg leg-1"></div>
              <div class="leg leg-2"></div>
            </div>
          </div>
          <div class="ground-line"></div>
        </div>
      </div>
      
      <!-- 加载进度 -->
      <div class="loading-progress">
        <div class="progress-bar">
          <div class="progress-fill" :style="{ width: progress + '%' }"></div>
        </div>
        <div class="progress-text">{{ loadingText }}</div>
        <div class="progress-percentage">{{ Math.round(progress) }}%</div>
      </div>
      
      <!-- 加载提示 -->
      <div class="loading-tips">
        <div class="tip-item" v-if="currentTip">
          <i class="icon-tip"></i>
          <span>{{ currentTip }}</span>
        </div>
      </div>
    </div>
    
    <!-- 背景粒子效果 -->
    <div class="particle-background">
      <div 
        v-for="n in 20" 
        :key="n" 
        class="particle"
        :style="getParticleStyle(n)"
      ></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'

/**
 * 游戏加载界面组件
 * 显示游戏加载进度和提示信息
 * @component
 */

interface Props {
  /** 加载进度 (0-100) */
  progress?: number
  /** 加载文本 */
  loadingText?: string
  /** 是否淡出 */
  fadeOut?: boolean
  /** 显示提示 */
  showTips?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  progress: 0,
  loadingText: '正在加载游戏...',
  fadeOut: false,
  showTips: true
})

// 游戏提示列表
const gameTips = [
  '使用空格键跳跃，避开障碍物',
  '按下方向键下可以滑铲通过低矮障碍',
  '收集金币可以解锁新角色',
  '不同角色有独特的能力和特点',
  '连续完美跳跃可以获得额外分数',
  '道具可以帮助你跑得更远',
  '挑战自己，创造新的记录！'
]

const currentTipIndex = ref(0)
const tipInterval = ref<number>()

// 计算属性
const currentTip = computed(() => {
  if (!props.showTips) return ''
  return gameTips[currentTipIndex.value]
})

/**
 * 获取粒子样式
 * @param index - 粒子索引
 * @returns 样式对象
 */
const getParticleStyle = (index: number) => {
  const size = Math.random() * 4 + 2
  const left = Math.random() * 100
  const animationDelay = Math.random() * 3
  const animationDuration = Math.random() * 3 + 2
  
  return {
    width: `${size}px`,
    height: `${size}px`,
    left: `${left}%`,
    animationDelay: `${animationDelay}s`,
    animationDuration: `${animationDuration}s`
  }
}

/**
 * 切换提示
 */
const switchTip = (): void => {
  currentTipIndex.value = (currentTipIndex.value + 1) % gameTips.length
}

// 生命周期钩子
onMounted(() => {
  if (props.showTips) {
    // 每3秒切换一次提示
    tipInterval.value = window.setInterval(switchTip, 3000)
  }
})

onUnmounted(() => {
  if (tipInterval.value) {
    clearInterval(tipInterval.value)
  }
})
</script>

<style lang="scss" scoped>
@import '@/styles/responsive.scss';

.loading-screen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 0.5s ease-out;
  
  &.fade-out {
    opacity: 0;
    pointer-events: none;
  }

  .loading-container {
    position: relative;
    z-index: 10;
    text-align: center;
    max-width: 500px;
    width: 90%;
    
    @include mobile-all {
      max-width: 350px;
    }
  }

  // 游戏Logo
  .game-logo {
    margin-bottom: 3rem;
    
    @include mobile-all {
      margin-bottom: 2rem;
    }

    .logo-icon {
      width: 80px;
      height: 80px;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0 auto 1rem;
      backdrop-filter: blur(10px);
      border: 2px solid rgba(255, 255, 255, 0.3);
      animation: logoFloat 3s ease-in-out infinite;
      
      @include mobile-all {
        width: 60px;
        height: 60px;
      }

      i {
        font-size: 2.5rem;
        color: white;
        
        @include mobile-all {
          font-size: 2rem;
        }
      }
    }

    .logo-text {
      color: white;
      font-size: 2.5rem;
      font-weight: 900;
      margin: 0;
      text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.4);
      letter-spacing: -1px;
      
      @include mobile-all {
        font-size: 2rem;
      }
      
      @include mobile-small {
        font-size: 1.8rem;
      }
    }
  }

  // 跑步动画
  .loading-animation {
    margin-bottom: 3rem;
    
    @include mobile-all {
      margin-bottom: 2rem;
    }

    .runner-animation {
      position: relative;
      height: 80px;
      
      @include mobile-all {
        height: 60px;
      }

      .runner {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        animation: runnerBounce 0.8s ease-in-out infinite;

        .runner-body {
          width: 20px;
          height: 30px;
          background: white;
          border-radius: 10px 10px 5px 5px;
          position: relative;
          
          @include mobile-all {
            width: 16px;
            height: 24px;
          }

          &::before {
            content: '';
            position: absolute;
            top: -8px;
            left: 50%;
            transform: translateX(-50%);
            width: 12px;
            height: 12px;
            background: white;
            border-radius: 50%;
            
            @include mobile-all {
              width: 10px;
              height: 10px;
              top: -6px;
            }
          }
        }

        .runner-legs {
          position: absolute;
          bottom: -15px;
          left: 50%;
          transform: translateX(-50%);
          
          @include mobile-all {
            bottom: -12px;
          }

          .leg {
            width: 3px;
            height: 15px;
            background: white;
            position: absolute;
            border-radius: 2px;
            
            @include mobile-all {
              width: 2px;
              height: 12px;
            }

            &.leg-1 {
              left: -4px;
              animation: legMove1 0.8s ease-in-out infinite;
            }

            &.leg-2 {
              right: -4px;
              animation: legMove2 0.8s ease-in-out infinite;
            }
          }
        }
      }

      .ground-line {
        position: absolute;
        bottom: 10px;
        left: 0;
        right: 0;
        height: 2px;
        background: rgba(255, 255, 255, 0.3);
        
        &::before {
          content: '';
          position: absolute;
          top: 0;
          left: 0;
          height: 100%;
          width: 50px;
          background: rgba(255, 255, 255, 0.6);
          animation: groundMove 1s linear infinite;
        }
      }
    }
  }

  // 加载进度
  .loading-progress {
    margin-bottom: 2rem;
    
    @include mobile-all {
      margin-bottom: 1.5rem;
    }

    .progress-bar {
      width: 100%;
      height: 8px;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 4px;
      overflow: hidden;
      margin-bottom: 1rem;
      border: 1px solid rgba(255, 255, 255, 0.3);
      
      @include mobile-all {
        height: 6px;
      }

      .progress-fill {
        height: 100%;
        background: linear-gradient(90deg, #4CAF50, #45a049);
        transition: width 0.3s ease;
        box-shadow: 0 0 10px rgba(76, 175, 80, 0.5);
        animation: progressGlow 2s ease-in-out infinite;
      }
    }

    .progress-text {
      color: white;
      font-size: 1.1rem;
      margin-bottom: 0.5rem;
      font-weight: 500;
      
      @include mobile-all {
        font-size: 1rem;
      }
    }

    .progress-percentage {
      color: rgba(255, 255, 255, 0.8);
      font-size: 0.9rem;
      font-family: 'Courier New', monospace;
      
      @include mobile-all {
        font-size: 0.8rem;
      }
    }
  }

  // 加载提示
  .loading-tips {
    .tip-item {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 0.75rem;
      background: rgba(255, 255, 255, 0.1);
      backdrop-filter: blur(10px);
      padding: 1rem 1.5rem;
      border-radius: $border-radius-lg;
      color: rgba(255, 255, 255, 0.9);
      border: 1px solid rgba(255, 255, 255, 0.2);
      animation: tipFadeIn 0.5s ease-in-out;
      
      @include mobile-all {
        padding: 0.875rem 1.25rem;
        flex-direction: column;
        gap: 0.5rem;
        text-align: center;
      }

      i {
        font-size: 1.2rem;
        color: rgba(255, 255, 255, 0.8);
        flex-shrink: 0;
      }

      span {
        font-size: 0.95rem;
        font-weight: 500;
        line-height: 1.4;
        
        @include mobile-all {
          font-size: 0.9rem;
        }
      }
    }
  }

  // 背景粒子
  .particle-background {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 1;

    .particle {
      position: absolute;
      background: rgba(255, 255, 255, 0.3);
      border-radius: 50%;
      animation: particleFloat 5s linear infinite;
    }
  }
}

// 动画定义
@keyframes logoFloat {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-10px);
  }
}

@keyframes runnerBounce {
  0%, 100% {
    transform: translate(-50%, -50%) translateY(0px);
  }
  50% {
    transform: translate(-50%, -50%) translateY(-5px);
  }
}

@keyframes legMove1 {
  0%, 100% {
    transform: rotate(0deg);
  }
  50% {
    transform: rotate(20deg);
  }
}

@keyframes legMove2 {
  0%, 100% {
    transform: rotate(0deg);
  }
  50% {
    transform: rotate(-20deg);
  }
}

@keyframes groundMove {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100vw);
  }
}

@keyframes progressGlow {
  0%, 100% {
    box-shadow: 0 0 10px rgba(76, 175, 80, 0.5);
  }
  50% {
    box-shadow: 0 0 20px rgba(76, 175, 80, 0.8);
  }
}

@keyframes tipFadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes particleFloat {
  0% {
    transform: translateY(100vh) rotate(0deg);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateY(-100px) rotate(360deg);
    opacity: 0;
  }
}

// 图标样式
.icon-game::before { content: '🎮'; }
.icon-tip::before { content: '💡'; }
</style>